// Package database @program: chat
//@author: edte
//@create: 2021-12-28 20:47
package database

import (
	log2 "chat/log"
	"chat/tools"
	"context"
	"errors"
	"fmt"
	"github.com/go-redis/redis/v8"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"strconv"
)

func MasterAddUser(ctx context.Context, roomId int, userId int, masterId int) error {
	mysqlPath := tools.GetEnv("MYSQL_PATH", "localhost:3306")
	mysqlUser := tools.GetEnv("MYSQL_USER", "root")
	mysqlPassword := tools.GetEnv("MYSQL_PASSWORD", "123456")
	db, err := gorm.Open(mysql.Open(mysqlUser+":"+mysqlPassword+"@tcp("+mysqlPath+")/chats?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})

	//db, err := gorm.Open(mysql.Open("root:123456@tcp(192.168.88.128:3306)/chats?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "MasterAddUser", "userid:"+strconv.Itoa(userId)+" roomid:"+strconv.Itoa(roomId)+" masterid:"+strconv.Itoa(masterId), "Error", "failed to connect database")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return errors.New("failed to connect database")
	}
	tmpChatRoom := tools.ChatRoomStruct{}
	db.Model(tools.ChatRoomStruct{}).Where("room_id=?", roomId).First(&tmpChatRoom)
	if tmpChatRoom.RoomId == 0 {

		writeLog, err2 := log2.WriteLog(ctx, "chat", "MasterAddUser", "userid:"+strconv.Itoa(userId)+" roomid:"+strconv.Itoa(roomId)+" masterid:"+strconv.Itoa(masterId), "Warring", "Chat room does not exist")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return errors.New("chat room does not exist")
	}

	if tmpChatRoom.MasterId != masterId {
		log.Println(tmpChatRoom.RoomId)
		log.Println(masterId)
		writeLog, err2 := log2.WriteLog(ctx, "chat", "MasterAddUser", "userid:"+strconv.Itoa(userId)+" roomid:"+strconv.Itoa(roomId)+" masterid:"+strconv.Itoa(masterId), "Warring", "No permission to add a person")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return errors.New("no permission to add a person")
	}
	redisPath := tools.GetEnv("REDIS_PATH", "localhost:6379")
	redisPD := tools.GetEnv("REDIS_PASSWORD", "")
	rdb := redis.NewClient(&redis.Options{
		Addr:     redisPath,
		Password: redisPD, // no password set
		DB:       2,       // use default DB
		//PoolSize: 500, // 连接池大小
	})

	_, err = rdb.SAdd(ctx, strconv.Itoa(roomId), userId).Result()
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "MasterAddUser", "userid:"+strconv.Itoa(userId)+" roomid:"+strconv.Itoa(roomId)+" masterid:"+strconv.Itoa(masterId), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return err
	}
	result, err := rdb.SIsMember(ctx, strconv.Itoa(roomId), userId).Result()
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "MasterAddUser", "userid:"+strconv.Itoa(userId)+" roomid:"+strconv.Itoa(roomId)+" masterid:"+strconv.Itoa(masterId), "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return err
	}
	if result == false {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "MasterAddUser", "userid:"+strconv.Itoa(userId)+" roomid:"+strconv.Itoa(roomId)+" masterid:"+strconv.Itoa(masterId), "Warring", "Failed to join chat room")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return errors.New("failed to join chat room")
	}
	return err
}
